package com.stock.demo.framework.ui.fragment;

import android.util.SparseArray;

import com.stock.demo.function.home.view.HomeFragment;
import com.stock.demo.function.market.view.MarketFragment;
import com.stock.demo.function.selfselect.view.SelfSelectFragment;
import com.stock.demo.function.trade.view.TradeFragment;

/**
 * Created by XuZhaoyu on 2017/6/1.
 */

public class FragmentFactory {

    private FragmentFactory() {

    }

    private static SparseArray<BaseFragment> sFragmentArray;

    public static synchronized BaseFragment getFragment(int position) {
        if (sFragmentArray == null) {
            sFragmentArray = new SparseArray<>();
        }
        BaseFragment fragment = sFragmentArray.get(position);
        if (fragment == null) {
            switch (position) {
                case 0:
                    fragment = new HomeFragment();
                    break;
                case 1:
                    fragment = new MarketFragment();
                    break;
                case 2:
                    fragment = new SelfSelectFragment();
                    break;
                case 3:
                    fragment = new TradeFragment();
                    break;
                //case 4:
                //fragment = new MineFragment();
                //break;
            }
            if (fragment != null) {
                sFragmentArray.put(position, fragment);
            }
        }
        return fragment;
    }

    public static void clearAll() {
        sFragmentArray = null;
    }

}
